﻿'use strict';
//10.19新增
var jsPath = CONFIG.jsPath;
angular.module('app').run(function($rootScope, $state, $stateParams, $localStorage) {
    $rootScope.$state = $state;
    $rootScope.$stateParams = $stateParams;
    //路由跳转前先判断令牌token是否存在
    $rootScope.$on('$stateChangeStart',function(event, toState){
        $rootScope.isLoading=true;
        if(toState&&(toState.name=='login'||toState.name=='404')){
            return;// 如果是进入登录界面则不需要校验
        }
        //如果令牌不存在
        if(!$localStorage.token){
            // 取消默认跳转
            event.preventDefault();
            $state.go("login");
            $rootScope.isLoading=false;
        }
    });
    $rootScope.$on('$stateChangeSuccess',function(event, toState){
        $rootScope.isLoading=false;
    });


}).config(function($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/login');
    $stateProvider
        .state('app', {
            abstract : true,
            url : '/app',
            controller : "layoutCtrl",
            templateUrl : 'views/layout.html',
            resolve : {
                deps : [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load(['wtmapModule','toaster']).then(
                            function() {
                                return $ocLazyLoad.load({
                                    serie: true,
                                    files: [
                                        jsPath + '/controllers/layout.js'
                                    ]
                                });
                            }
                        );
                    }
                ]
            }
        })
        //登录
        .state('login', {
            url: '/login',
            templateUrl: 'views/login.html',
            ncyBreadcrumb: {
                label: '登录'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load('toaster').then(function() {
                            return $ocLazyLoad.load({
                                serie: true,
                                files: [
                                    // 'lib/jquery/canvas/particle.js',
                                    jsPath + '/controllers/account.js',
                                    jsPath + '/services/account.js'
                                ]
                            });
                        });
                    }
                ]
            }
        })
        .state('app.home', {
            url: '/home',
            templateUrl: 'views/home.html',
            controller: 'homeCtrl',
            ncyBreadcrumb: {
                label: '系统首页'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load('ngTagsInput').then(
                            function() {
                                return $ocLazyLoad.load({
                                    serie: true,
                                    files: [
                                        jsPath + '/controllers/home.js',
                                    ]
                                });
                            }
                        )
                    }
                ]
            }
        })
        .state('app.user', {
            url: '/user',
            templateUrl: 'views/user.html',
            controller: 'userCtrl',
            ncyBreadcrumb: {
                label: '用户管理'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            serie: true,
                            files: [
                                jsPath + '/controllers/user.js', //引入ctrl
                            ]
                        })
                    }
                ]
            }
        })
        .state('app.device', {
            url: '/device',
            templateUrl: 'views/device.html',
            controller: 'deviceCtrl',
            ncyBreadcrumb: {
                label: '设施管理'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            serie: true,
                            files: [
                                // jsPath + '/directives/wtmapGetPosition.js', //引入选择位置指令
                                jsPath + '/controllers/device.js', //引入ctrl

                            ]
                        });
                    }
                ]
            }
        })
        //隐患管理
        .state('app.danger', {
            url: '/danger',
            templateUrl: 'views/danger.html',
            controller: 'dangerCtrl',
            ncyBreadcrumb: {
                label: '隐患管理'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            serie: true,
                            files: [
                                // jsPath + '/directives/wtmapSetLocation.js', //引入标注位置指令
                                // jsPath + '/directives/wtmapGetPosition.js', //引入设置设置坐标点指令
                                jsPath + '/controllers/danger.js', //引入ctrl
                            ]
                        });
                    }
                ]
            }

        })
        //工作记录===>修改成系统日志
        .state('app.log', {
            url: '/log',
            templateUrl: 'views/log.html',
            ncyBreadcrumb: {
                label: '工作记录'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            serie: true,
                            files: [
                                'app/controllers/log.js',
                                'app/services/log.js',
                                'app/directives/whenScrolled.js'
                            ]
                        });
                    }
                ]
            }

        })
        //工作记录(巡查维护)
        .state('app.record', {
            url: '/record',
            templateUrl: 'views/record.html',
            controller: 'recordCtrl',
            ncyBreadcrumb: {
                label: '工作记录'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            serie: true,
                            files: [
                                jsPath + '/directives/wtmapSetLocation.js', //引入标注位置指令
                                jsPath + '/controllers/record.js', //引入ctrl
                            ]
                        });
                    }
                ]
            }

        })
        //电子地图
        .state('app.map', {
            url: '/map',
            templateUrl: 'views/map.html',
            //controller : 'MapCtrl',
            ncyBreadcrumb: {
                label: '电子地图'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            serie: true,
                            files: [

                                jsPath + '/services/mapService.js',
                                jsPath + '/controllers/map.js'
                            ]
                        });
                    }
                ]
            }

        })
        //巡查管理
        .state('app.inspection', {
            url: '/inspection',
            templateUrl: 'views/inspection.html',
            controller: 'inspectionCtrl',
            ncyBreadcrumb: {
                label: '巡查管理'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            serie: true,
                            files: [
                                jsPath + '/controllers/inspection.js'
                            ]
                        });
                    }
                ]
            }

        })
        //巡查管理
        .state('app.servicing', {
            url: '/servicing',
            templateUrl: 'views/servicing.html',
            controller: 'servicingCtrl',
            ncyBreadcrumb: {
                label: '维修管理'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            serie: true,
                            files: [
                                jsPath + '/directives/wtmapSetLocation.js', //引入选择位置指令
                                jsPath + '/controllers/servicing.js',
                                'lib/jquery/fuelux/wizard/wizard-custom.js'
                            ]
                        });
                    }
                ]
            }

        })
        //周期计划
        .state('app.plan', {
            url: '/plan',
            templateUrl: 'views/plan.html',
            controller: 'planCtrl',
            ncyBreadcrumb: {
                label: '周期计划'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load(['ui.select', 'ngTagsInput']).then(
                            function() {
                                return $ocLazyLoad.load({
                                    serie : true,
                                    files : [
                                        jsPath + '/directives/wtmapSetLocation.js',
                                    ]
                                })
                            }).then(function(){
                                return $ocLazyLoad.load({
                                    serie : true,
                                    files : [
                                        jsPath + '/controllers/plan.js',
                                    ]
                                });
                            }
                        );
                    }
                ]
            }
        })
        //故障管理
        .state('app.fault', {
            url: '/fault',
            templateUrl: 'views/fault.html',
            controller: 'faultCtrl',
            ncyBreadcrumb: {
                label: '故障管理'
            },
            resolve: {
                deps: [
                    '$ocLazyLoad',
                    function($ocLazyLoad) {
                        return $ocLazyLoad.load({
                            serie: true,
                            files: [
                                jsPath + '/controllers/fault.js'
                            ]
                        });
                    }
                ]
            }
        })
        .state('app.im', {
            url: '/im',
            templateUrl: 'views/im.html',
            ncyBreadcrumb: {
                label: '通讯'
            },
        })
});
